package com.yanqu.road.server.cmd.cross;

import com.yanqu.road.entity.enums.eSystemId;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.union.cross.CrossUnionProto;
import com.yanqu.road.pb.worldmessage.WorldMessageProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.group.SystemGroupChatMgr;
import com.yanqu.road.server.manager.player.WorldMessageMgr;
import com.yanqu.road.server.manger.CrossUnionServerGroupMgr;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.YanQuMessageUtils;

import java.util.ArrayList;
import java.util.List;

@Cmd(code = Protocol.C_CROSS_WORLD_CHAT_LIST, desc = "商会分组世界聊天列表")
public class CrossWorldChatListCmd extends CrossCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        int groupId = CrossUnionServerGroupMgr.getGroupIdByServerId(serverId);
        WorldMessageProto.WorldMessageListMsg.Builder listMsgBuilder = SystemGroupChatMgr.getChatListMsg(eSystemId.Union.getValue(), groupId);
        if(listMsgBuilder == null){
            listMsgBuilder = WorldMessageProto.WorldMessageListMsg.newBuilder();
        }
        CrossUnionProto.UnionMemberChatListCrossUploadMsg uploadMsg = CrossUnionProto.UnionMemberChatListCrossUploadMsg.parseFrom(packet.getBytes());
        List<WorldMessageProto.WorldMessageTempMsg.Builder> unionMessageList = new ArrayList<>();

        for (WorldMessageProto.WorldMessageTempMsg tempMsg : uploadMsg.getMsgListList()) {
            if(uploadMsg.getBlackListList().contains(tempMsg.getUserId())){
                continue;
            }
            unionMessageList.add(tempMsg.toBuilder());
        }

        for (WorldMessageProto.WorldMessageTempMsg tempMsg : listMsgBuilder.getDataListList()) {
            unionMessageList.add(tempMsg.toBuilder());
        }

        unionMessageList.sort((o1, o2) -> (int) (o1.getSendTime() - o2.getSendTime()));
        while (unionMessageList.size() > SystemGroupChatMgr.MAX_MESSAGE_COUNT) {
            unionMessageList.remove(0);
        }

        WorldMessageProto.WorldMessageListMsg.Builder systemListBuild = SystemGroupChatMgr.getSystemChatListMsg(eSystemId.Union.getValue(), groupId);
        if(systemListBuild != null){
            for (WorldMessageProto.WorldMessageTempMsg tempMsg : systemListBuild.getDataListList()) {
                unionMessageList.add(tempMsg.toBuilder());
            }
        }

        WorldMessageProto.WorldMessageListMsg.Builder messageList = WorldMessageProto.WorldMessageListMsg.newBuilder();
        for (WorldMessageProto.WorldMessageTempMsg.Builder builder : unionMessageList) {
            messageList.addDataList(builder);
        }
        MessageHelper.sendPacket(serverId, packet.getPlayerId(), YanQuMessageUtils.buildMessage(Protocol.U_WORLD_MESSAGE_LIST, messageList));

    }

    @Override
    public void sendData() {

    }
}
